package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
import com.ysd.dao.impl.ProductDaoImpl;
import com.ysd.entity.Product;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminProductServlet extends BaseServlet {
	/**
	 * 商品多条件查询 
	 * localhost:8080/meizizi/adminProduct?reqName=getProducts
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProducts(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接收前台传来的参数
		int productId = 0;
		if (request.getParameter("productId") != null && !request.getParameter("productId").equals("")) {
			productId = Integer.parseInt(request.getParameter("productId")); // NumberFormatException数字格式化异常
		}

		String productName = request.getParameter("productName");

		int marketPrice = 0;
		if (request.getParameter("marketPrice") != null && !request.getParameter("marketPrice").equals("")) {
			marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		}

		int beginmarketPrice = 0;
		if (request.getParameter("beginmarketPrice") != null && !request.getParameter("beginmarketPrice").equals("")) {
			beginmarketPrice = Integer.parseInt(request.getParameter("beginmarketPrice")); // NumberFormatException数字格式化异常
		}
		int endmarketPrice = 0;
		if (request.getParameter("endmarketPrice") != null && !request.getParameter("endmarketPrice").equals("")) {
			endmarketPrice = Integer.parseInt(request.getParameter("endmarketPrice")); // NumberFormatException数字格式化异常
		}

		String brand = request.getParameter("brand");
		String isDistribution = request.getParameter("isDistribution");
		String isCashPayment = request.getParameter("isCashPayment");
		String flavor = request.getParameter("flavor");
		String productImg = request.getParameter("productImg");

		int productCategoryId = 0;
		if (request.getParameter("productCategoryId") != null
				&& !request.getParameter("productCategoryId").equals("")) {
			productCategoryId = Integer.parseInt(request.getParameter("productCategoryId")); // NumberFormatException数字格式化异常
		}

		Product p = new Product(productId, productName, 0, beginmarketPrice, endmarketPrice, brand, isDistribution,
				isCashPayment, flavor, productImg, productCategoryId, "");
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		
		ProductDaoImpl pDao = new ProductDaoImpl();
		List<Product> pList = pDao.getProductsByWhere(p, page ,size);

		Gson gson = new Gson(); // 创建Gson对象
		String res = gson.toJson(pList); 

		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// 回信者 out对象

		out.print(res); // 响应数据

		out.flush(); // 清空并关闭
		out.close();
	}
	
	/**
	 * localhost:8080/meizizi/adminProduct?reqName=getAllProducts
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getAllProducts(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 接收前台传来的参数
		int productId = 0;
		if (request.getParameter("productId") != null && !request.getParameter("productId").equals("")) {
			productId = Integer.parseInt(request.getParameter("productId")); // NumberFormatException数字格式化异常
		}

		String productName = request.getParameter("productName");

		int marketPrice = 0;
		if (request.getParameter("marketPrice") != null && !request.getParameter("marketPrice").equals("")) {
			marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		}

		int beginmarketPrice = 0;
		if (request.getParameter("beginmarketPrice") != null && !request.getParameter("beginmarketPrice").equals("")) {
			beginmarketPrice = Integer.parseInt(request.getParameter("beginmarketPrice")); // NumberFormatException数字格式化异常
		}
		int endmarketPrice = 0;
		if (request.getParameter("endmarketPrice") != null && !request.getParameter("endmarketPrice").equals("")) {
			endmarketPrice = Integer.parseInt(request.getParameter("endmarketPrice")); // NumberFormatException数字格式化异常
		}

		String brand = request.getParameter("brand");
		String isDistribution = request.getParameter("isDistribution");
		String isCashPayment = request.getParameter("isCashPayment");
		String flavor = request.getParameter("flavor");
		String productImg = request.getParameter("productImg");

		int productCategoryId = 0;
		if (request.getParameter("productCategoryId") != null
				&& !request.getParameter("productCategoryId").equals("")) {
			productCategoryId = Integer.parseInt(request.getParameter("productCategoryId")); // NumberFormatException数字格式化异常
		}

		Product p = new Product(productId, productName, 0, beginmarketPrice, endmarketPrice, brand, isDistribution,
				isCashPayment, flavor, productImg, productCategoryId, "");		
		ProductDaoImpl pDao = new ProductDaoImpl();
		List<Product> pList = pDao.getProductsByWheres(p);

		Gson gson = new Gson(); // 创建Gson对象
		String res = gson.toJson(pList.size()); 

		response.setCharacterEncoding("UTF-8");// 设置编码格式
		PrintWriter out = response.getWriter();// 回信者 out对象

		out.print(res); // 响应数据

		out.flush(); // 清空并关闭
		out.close();
	}
	
	/**
	 * 根据产品id删除产品 localhost:8080/meizizi/adminProduct?reqName=deleteProduct
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void deleteProduct(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int cId = Integer.parseInt(request.getParameter("productId"));// 接收前台传来的参数
		ProductDaoImpl pDao = new ProductDaoImpl();
		int c = pDao.deleteProductById(cId); // 调用删除方法
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out对象
		if (c > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminProduct?reqName=addProductdetails
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addProductdetails(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		// 接收前台传来的参数
		String productName = request.getParameter("productName");

		int marketPrice = 0;
		if (request.getParameter("marketPrice") != null && !request.getParameter("marketPrice").equals("")) {
			marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		}

		String brand = request.getParameter("brand");
		String isDistribution = request.getParameter("isDistribution");
		String isCashPayment = request.getParameter("isCashPayment");
		String flavor = request.getParameter("flavor");
		String productImg = request.getParameter("productImg");

		int productCategoryId = 0;
		if (request.getParameter("productCategoryId") != null
				&& !request.getParameter("productCategoryId").equals("")) {
			productCategoryId = Integer.parseInt(request.getParameter("productCategoryId")); // NumberFormatException数字格式化异常
		}

		ProductDaoImpl pDao = new ProductDaoImpl(); // 创建产品详情的实现类对象
		if (productName != null && !productName.equals("")) { // 非空判定
			List<Product> pList = pDao.getProductDetailsByName("productName");
			if (pList.size() == 0) { // 没有查到可以添加
				Product p = new Product(0, productName, marketPrice, brand, isDistribution, isCashPayment, flavor,
						productImg, productCategoryId); // 根据接受的参数创建产品分类对象
				int x = pDao.addProductDetails(p);
				if (x > 0) {// 添加成功
					out.print(Result.toClient(true, "添加成功", ""));// {"success":true,"message":"添加成功","remark":""}
				} else {// 添加失败
					out.print(Result.toClient(false, "添加失败", ""));// {"success":false,"message":"添加失败","remark":""}
				}
			} else { // 查到了不能添加
				out.print(Result.toClient(false, "产品名称已存在", ""));
			}
		}

		out.flush();// 清空并关闭
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminProduct?reqName=upload
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		// 1.创建SmarUpload对象
		SmartUpload smart = new SmartUpload();
		// 2.设置编码格式
		smart.setCharset("UTF-8");
		// 3.初始化
		smart.initialize(getServletConfig(), request, response);

		try {// 正常
				// 4.上传
			smart.upload();
			// 5.设置上传文件名
			String fileName = System.currentTimeMillis() + ".jpg";
			// 6.将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("/images/" + fileName);
			// 7.响应数据
			out.print(Result.toClient(true, "上传成功", fileName));
		} catch (SmartUploadException e) {// 异常
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminProduct?reqName=getProductsById
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getProductsById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		ProductDaoImpl pDao = new ProductDaoImpl(); // 创建产品的实现类对象
		int id = Integer.parseInt(request.getParameter("cId")); // 接收前台传来的参数,并转化为int类型
		Product p = pDao.getProductDetailsById(id); // 调用查询方法查到结果

		Gson gson = new Gson();
		String res = gson.toJson(p); // 将p转化为json格式字符串

		out.print(res); // 响应数据

		out.flush();// 清空并关闭
		out.close();
	}

	/**
	 * localhost:8080/meizizi/adminProduct?reqName=updateProductsById
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateProductsById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8"); // 设置相应的编码格式
		PrintWriter out = response.getWriter(); // out对象

		ProductDaoImpl pDao = new ProductDaoImpl(); // 创建产品的实现类对象
		
		int productId = Integer.parseInt(request.getParameter("productId")); // 接收前台传来的参数,并转化为int类型
		String productName = request.getParameter("productName");
		int marketPrice = Integer.parseInt(request.getParameter("marketPrice"));
		String brand = request.getParameter("brand");
		String isDistribution = request.getParameter("isDistribution");
		String isCashPayment = request.getParameter("isCashPayment");
		String flavor = request.getParameter("flavor");
		String productImg = request.getParameter("productImg");
		int productCategoryId = Integer.parseInt(request.getParameter("productCategoryId"));
		
		List<Product> pList = pDao.getProductDetailsByName("productName");
		if (pList.size() == 0) { // 没有查到可以添加
			Product p = new Product(productId, productName, marketPrice, brand, isDistribution, isCashPayment, flavor, productImg, productCategoryId); // 根据接受的参数创建产品分类对象
			int x = pDao.updateProductDetailsById(p);
			if (x > 0) {// 添加成功
				out.print(Result.toClient(true, "添加成功", ""));// {"success":true,"message":"添加成功","remark":""}
			} else {// 添加失败
				out.print(Result.toClient(false, "添加失败", ""));// {"success":false,"message":"添加失败","remark":""}
			}
		} else { // 查到了不能添加
			out.print(Result.toClient(false, "产品名称已存在", ""));
		}

		out.flush();// 清空并关闭
		out.close();
	}
	/**
	 * 根据产品id删除产品 localhost:8080/meizizi/adminProduct?reqName=delProductByIds
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delProductByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out对象
		String ids=request.getParameter("ids");// 接收前台传来的参数
		ProductDaoImpl pDao = new ProductDaoImpl();
		int c = pDao.delProductByIds(ids); // 调用删除方法
		
		if (c > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}
}
